Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement encode/decode 1inch extension #75

Merged
merged 26 commits into from
Aug 21, 2024
Merged

Conversation

chrisngyn
Copy link
Member

No description provided.

@chrisngyn chrisngyn force-pushed the feature/add-1inch-extension branch 2 times, most recently from d95c6c7 to a982d2a Compare August 14, 2024 04:58
@chrisngyn chrisngyn changed the title Add encode/decode 1inch extension WIP: Add encode/decode 1inch extension Aug 14, 2024
@chrisngyn chrisngyn force-pushed the feature/add-1inch-extension branch 2 times, most recently from 732bd9c to 023ce6c Compare August 16, 2024 03:40
@chrisngyn chrisngyn changed the title WIP: Add encode/decode 1inch extension Implement encode/decode 1inch extension Aug 16, 2024
@chrisngyn chrisngyn force-pushed the feature/add-1inch-extension branch from 8e17469 to 30bd71f Compare August 16, 2024 08:09

type AddressHalf [addressHalfLength]byte

func HalfAddressFromAddress(a common.Address) AddressHalf {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mostly we never modify an address, I think get a slice of bytes will save some allocation.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because AddressHalf is a static slice of bytes, same as common.Address. So I decided still keep it.

// Logic is copied from
// https://etherscan.io/address/0xfb2809a5314473e1165f6b58018e20ed8f07b840#code#F23#L140
// nolint: gomnd
func DecodeAuctionDetails(hexData []byte) (AuctionDetails, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should check input has enough length for decode operation.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have updated

points := make([]AuctionPoint, 0)
for len(data) > 0 {
coefficient := new(big.Int).SetBytes(data[:3]).Int64()
delay := new(big.Int).SetBytes(data[3:5]).Int64()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should validate slice access.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have updated

})

// nolint: lll
t.Run("decode 2", func(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should test for bad data decode also

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added other test for bad data

return tmp
}

func encodeInt64ToBytes(n int64, size int) []byte {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

take a look at math.PaddedBigBytes and math.U256Bytes, it may provided same function

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have refactored to use math.PaddingBigBytes function

var customReceiver common.Address

if resolverFeeEnabled(flags) {
bankFee.SetBytes(data[:4])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check for index out of range access


if integratorFeeEnabled(flags) {
integratorFeeRatio := new(big.Int).SetBytes(data[:2]).Int64()
integratorAddress := common.BytesToAddress(data[2:22])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check for index out of range access

@chrisngyn chrisngyn requested a review from hiepnv90 August 16, 2024 09:27
@chrisngyn chrisngyn force-pushed the feature/add-1inch-extension branch from 875e535 to 4e93168 Compare August 16, 2024 10:47
pkg/oneinch/decode/decode.go Outdated Show resolved Hide resolved
pkg/oneinch/decode/decode.go Outdated Show resolved Hide resolved
pkg/oneinch/fusionorder/auction_details.go Outdated Show resolved Hide resolved
pkg/oneinch/fusionorder/auction_details.go Outdated Show resolved Hide resolved
pkg/oneinch/fusionorder/auction_details.go Outdated Show resolved Hide resolved
pkg/oneinch/limitorder/extension_test.go Show resolved Hide resolved
pkg/oneinch/fusionorder/auction_details_test.go Outdated Show resolved Hide resolved
@chrisngyn chrisngyn force-pushed the feature/add-1inch-extension branch from 4e93168 to c2cc714 Compare August 19, 2024 04:26
return 0, nil, err
}

return new(big.Int).SetBytes(d).Int64(), remainingData, nil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's use binary.BigEndian.Uint64() for reading int64

return AuctionDetails{}, fmt.Errorf("next offset: %w", err)
}

gasBumpEstimate := new(big.Int).SetBytes(offsetData[:3]).Int64()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let use binary.BigEndian.Uint64() for reading int value <= 64 bit


offsetsBytes := e.getOffsets()
paddedOffsetHex := fmt.Sprintf("%064x", offsetsBytes)
return zx + paddedOffsetHex + hex.EncodeToString(interactionsConcatenated) + hex.EncodeToString(e.CustomData)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should combine into []byte and use hexutil.Encode once.

@chrisngyn chrisngyn force-pushed the feature/add-1inch-extension branch from 8457df1 to d520732 Compare August 19, 2024 08:00
pkg/oneinch/limitorder/extension.go Outdated Show resolved Hide resolved
pkg/oneinch/limitorder/extension.go Outdated Show resolved Hide resolved
pkg/oneinch/limitorder/extension.go Show resolved Hide resolved
Copy link
Member

@hiepnv90 hiepnv90 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! 👍

@chrisngyn chrisngyn merged commit 213cb3b into main Aug 21, 2024
3 checks passed
@chrisngyn chrisngyn deleted the feature/add-1inch-extension branch August 21, 2024 06:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants